﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using YAPL.Language.Ast.Utils;

namespace YAPL.Language.Ast {
	public class UnaryExpression : Expression {
		private Expression _operand;
		private readonly UnaryOperator _operator;

		public UnaryExpression(ExpressionType nodeType, Type type, Expression operand, UnaryOperator op)
			: base(nodeType, type) {
			_operand = operand;
			_operator = op;
		}

		public UnaryExpression(ExpressionType nodeType, Type type, bool canRead, bool canWrite, Expression operand, UnaryOperator op)
			: base(nodeType, type, canRead, canWrite) {
			_operand = operand;
			_operator = op;
		}

		[Descendant(AstVisit.BeforeCurrent)]
		public Expression Operand {
			get { return _operand; }
		}

		public UnaryOperator Operator {
			get { return _operator; }
		}

		public override bool NodeEquals(Expression other) {
			if (other is UnaryExpression) {
				return _operator.Equals((other as UnaryExpression)._operator);
			}
			return false;
		}

		public override bool RewriteChild(Expression child, Expression newExpression) {
			if (ReferenceEquals(_operand, child)) {
				_operand = newExpression;
				return true;
			}
			
			return false;
		}

	}
}
